/**********************************************************************************
 * Copyright (c) 2012 - 2012,  quick_tools, All right reserved
 * Title: com.adc.quicktools  ListUtil.java 
 * @author shen  shenqiao2012@163.com 
 *  2012-7-17 上午11:04:31 
 *********************************************************************************/


package com.adc.quicktools;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;



public class ListUtil {
	
	/**
	 * 
	 * Description: 对简单类型的list进行自然排序
	 * author:  shen
	 * @param list
	 * @return List
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List autoNatureSort(List list) {
		List tempList=new ArrayList(list);
		Collections.sort(tempList);
		return tempList;
	}

	/**
	 * Description: 对 普通list进行去重  及  自然排序
	 * @param list  
	 * @Return List
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List autoNatureSortAndUnique(List  list) {
		List temp =new ArrayList();
		Set set =new HashSet();
		set.addAll(list);
		for (Iterator it = set.iterator() ; it.hasNext();  ) {
			temp.add(it.next());
		}
		Collections.sort(temp);
		return temp;
	}
	
	/**
	 * 
	 * Description: 对list进行自然排序
	 * author:  shen
	 * @param list
	 * @return List
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List autoNatureSortForComplex(List list) {
		List tempList=new ArrayList(list);
		Collections.sort(tempList,new ListComparator());
		return tempList;
	}
	
	/**
	 * 
	 * Description: 根据指定方法名对list进行自然排序
	 * author:  shen
	 * @param list
	 * @return List
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List autoNatureSortForComplex(List list,String methodName) {
		List tempList=new ArrayList(list);
		Collections.sort(tempList,new ListComparator(methodName));
		return tempList;
	}
	
	/**
	 * 
	 * Description: 对integer的list进行自然排序
	 * author:  shen
	 * @param list
	 * @return List
	 */
	public static List<Integer> autoNatureSortForInteger(List<Integer> list) {
		List<Integer> tempList=new ArrayList<Integer>(list);
		Collections.sort(tempList,new IntegerComparator());
		return tempList;
	}
	
	

}

class IntegerComparator implements Comparator<Integer>{

	/**
	 * Description:  
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 
	 */
	@Override
	public int compare(Integer o1, Integer o2) {
		int result;
		if (o1>o2) {
			result=-1;
		}else if(o1<o2){
			result=1;
		}else {
			result=0;
		}
		return result;
	}
	
}
